#include "../include/serverLogin.h"
#include "../include/head.h"

int server_register(int sockfd);
int server_login(int sockfd,char *username_file);
int server_logout(int sockfd);

//模式选择
int handle_client_request(int sockfd,Button *button,char *username_file)
{ 
    recv(sockfd,button,sizeof(int *),0);
    printf("button = %d\n",*button);
    switch(*button)
    { 
    case REGISTER:
        printf("do register\n");
        server_register(sockfd);
        break;
    case LOGIN:
        printf("do login\n");
        server_login(sockfd,username_file);
        break;
    case LOGOUT:
        printf("do logout\n");
        server_logout(sockfd);
        break;
    default:
        break;
    }
    return 0;
}
//主程序调用这个
int serLog(int sockfd,char *username_file)
{ 
    Button button;
    handle_client_request(sockfd,&button,username_file);
    return 0;
}

int loginTrainsend(int sockfd, const char buf[]) {
    logTrain_t train;
    bzero(train.data, sizeof(train.data));
    train.length = strlen(buf) + 1;
    strcpy(train.data, buf);
    send(sockfd, &train.length, sizeof(train.length), 0);
    send(sockfd, train.data, train.length, 0);
    return 0;
}

int loginTrainRecv(int sockfd, char buf[]){
    logTrain_t train;
    bzero(train.data, sizeof(train.data));
    recv(sockfd, &train.length, sizeof(train.length) , 0);
    recv(sockfd, train.data, train.length, MSG_WAITALL);
    strcpy(buf, train.data);
    return 0;
}
//注册模块
int server_register(int sockfd)
{ 
    char recvName[1024] = {0};
    char salt[1024] = {0};
    get_salt(salt);
    char encrypted_password[1024] = {0};

    //收用户名火车
    loginTrainRecv(sockfd,recvName);
    printf("收到用户名：%s\n",recvName);
    //向数据库插入用户名
    mysql_userInfo_Insert_userName(recvName);

    //将盐值插入数据库
    mysql_userInfo_Insert_salt(salt,recvName);

    //发盐值火车
    loginTrainsend(sockfd,salt);
    printf("salt = %s\n",salt);
    //收密文密码
    loginTrainRecv(sockfd,encrypted_password);
    mysql_userInfo_Insert_encryted_password(encrypted_password,recvName);
    printf("miwen:%s\n", encrypted_password);

    loginTrainsend(sockfd,"注册成功");
    mysql_file_Insert_userName(recvName);
    return 0;
}
//登录模块
int server_login(int sockfd,char *username_file)
{ 
    printf("login\n");
    int login_success = 0;
    char tempbuf[1024] = {0};
    while(login_success == 0){
        printf("in while 1\n");
        char userName[1024] = {0};

        //收用户名火车
        loginTrainRecv(sockfd,userName);
        printf("收到用户名：%s\n",userName);

        printf("调用前 - userName地址: %p, 内容: '%s'\n", userName, userName);

        //从数据库找用户名
        mysql_userInfo_takeOut_name(userName,tempbuf);
       
        printf("table_name = %s\n",tempbuf);
        if(tempbuf[0] == '\0'){
            //用户不存在
            int isExist = 0;

            //发送响应
            send(sockfd,&isExist,sizeof(isExist),0);
            continue;
        } else{
            //用户存在
            int isExist = 1;
            int pswd_succ = 0;
            char salt[1024] = {0};

            //发送响应
            send(sockfd,&isExist,sizeof(isExist),0);

            //发送盐值                                                           
            mysql_userInfo_takeOut_salt(tempbuf,salt);
            loginTrainsend(sockfd,salt);

            while(pswd_succ == 0){
                char new_crypt[4096] = {0};
                char tempbuf[4096] = {0};   
                //接收用户发来的自己加密的密文
                loginTrainRecv(sockfd,new_crypt);

                /* printf("new_crypt =%s\n",new_crypt);//测试 */

                //从数据库表中取出正确的密文密码
                mysql_userInfo_takeOut_encryted_password(userName,tempbuf);

                if(strcmp(new_crypt,tempbuf) == 0){
                    strcpy(username_file,userName);
                    int isExist = 1;
                    //说明用户名和密码正确，登陆成功
                    send(sockfd,&isExist,sizeof(isExist),0);
                    login_success = 1;
                    pswd_succ = 1;
                    return 1;

                } else{
                    //说明密码不正确，登陆失败
                    int isExist = 0;
                    send(sockfd,&isExist,sizeof(isExist),0);
                }
            }
        }
    }
    return 0;
}
int server_logout(int sockfd)
{ 
    char recvName[1024] = {0};
    char salt[1024] = {0};
    get_salt(salt);
    char tempbuf[1024] = {0};
    //收用户名火车
    loginTrainRecv(sockfd,recvName);
    printf("收到用户名：%s\n",recvName);
    //找名字回
    int ret = mysql_userInfo_takeOut_name(recvName,tempbuf);
    if( ret == 0 )
    { 
        //用户不存在
        int isExist = 0;
        //发响应
        send(sockfd,&isExist,sizeof(isExist),0);
    }else
    { 
        //用户存在
        int isExist = 1;
        //发响应
        send(sockfd,&isExist,sizeof(isExist),0);

        //发盐值火车
        mysql_userInfo_takeOut_salt(recvName,tempbuf);
        loginTrainsend(sockfd,tempbuf);


        //收密文密码
        char new_crypt[4096] = {0};
        loginTrainRecv(sockfd,new_crypt);
        //找密文密码
        char table_pswd[4096] = {0};
        mysql_userInfo_takeOut_encryted_password(recvName,table_pswd);
        if(strcmp(new_crypt,table_pswd) == 0)
        {
            //密码正确
            //发注销成功响应
            mysql_userInfo_updata_isDeleted(recvName); 
            char deletedMsg[] = "注销成功！";
            loginTrainsend(sockfd,deletedMsg);
        }
        else
        {
            //密码不正确
            //发送注销失败响应
            char deletedMsg[] = "注销失败！" ;
            loginTrainsend(sockfd,deletedMsg);
        }
    }
    return 0;
}



